﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shove.Web.UI;

public partial class UserControls_FillShopInfo : UCBase
{
    public UserControls_FillShopInfo()
    {
        Name = "收货信息";

        swpas = new ShoveWebPartAttribute[23];

        swpas[0] = new ShoveWebPartAttribute_Int("控件宽度", "", 759, "控件宽度");
        swpas[1] = new ShoveWebPartAttribute_Int("导航高度", "1,300", 23, "导航高度");
        swpas[2] = new ShoveWebPartAttribute_Int("标题行高", "1,500", 26, "标题行高");
        swpas[3] = new ShoveWebPartAttribute_Int("文本框宽度", "1,500", 177, "文本框宽度");

        swpas[4] = new ShoveWebPartAttribute_Image("导航单元格图片1", "", "images/UserControls/bg_gold_23_2.jpg", "导航单元格图片1");
       
        swpas[5] = new ShoveWebPartAttribute_Text("导航文本样式", "", "black12", "导航文本样式");
        swpas[6] = new ShoveWebPartAttribute_Text("提示标题样式", "", "black12", "提示标题样式");
        swpas[7] = new ShoveWebPartAttribute_Text("注意提示文本样式", "", "help12", "注意提示文本样式");
        swpas[8] = new ShoveWebPartAttribute_Text("文本框样式", "", "in_p2", "文本框样式");

        swpas[9] = new ShoveWebPartAttribute_Color("控件边框颜色", "#C5E4FF", "控件边框颜色");

        swpas[10] = new ShoveWebPartAttribute_Int("配送方式样式行高","1,300", 26, "配送方式样式行高");
        swpas[11] = new ShoveWebPartAttribute_Text("配送方式样式", "", "black12", "配送方式样式");

        swpas[12] = new ShoveWebPartAttribute_Int("支付方式样式行高", "1,300", 26, "支付方式样式行高");
        swpas[13] = new ShoveWebPartAttribute_Text("支付方式样式", "", "black12", "支付方式样式");
        swpas[14] = new ShoveWebPartAttribute_Text("支付内容样式", "", "black12", "支付内容样式");
        swpas[15] = new ShoveWebPartAttribute_Text("商品明细样式","","price","商品明细样式");

        swpas[16] = new ShoveWebPartAttribute_Int("表头高度", "1,300", 23, "表头高度");
        swpas[17] = new ShoveWebPartAttribute_Int("每页显示个数", "1,20", 5, "每页显示个数");
        swpas[18] = new ShoveWebPartAttribute_Text("表格内容样式", "", "black12", "表格内容样式");
        swpas[19] = new ShoveWebPartAttribute_Text("表头CSS样式", "", "", "表头CSS样式");
        swpas[20] = new ShoveWebPartAttribute_Text("订单总金额样式","","price","订单总金额样式");
        swpas[21] = new ShoveWebPartAttribute_Image("上一步图片", "", "images/UserControls/button_up.jpg", "上一步图片");
        swpas[22] = new ShoveWebPartAttribute_Image("下一步按钮图片", "", "images/UserControls/button_next.jpg", "下一步按钮图片");
    }

    private void InitLayOut()
    {
        //tabctr.Width = swpAttributes[0];
        //tabctr.BgColor = swpAttributes[9];

        //tdsign.Height = swpAttributes[1];
        //tdsign.Attributes.Add("class", swpAttributes[5]);
        //tdsign.Style.Add(HtmlTextWriterStyle.BackgroundImage, "url('" + swpAttributes[4] + "')");

        tdname.Height = swpAttributes[2];
        tdname.Attributes.Add("class", swpAttributes[6]);
        tdname1.Attributes.Add("class", swpAttributes[7]);
        txtConsigneeName.Width = new Unit(swpAttributes[3]);
        txtConsigneeName.CssClass = swpAttributes[8];

        tdemial.Height = swpAttributes[2];
        tdemial.Attributes.Add("class", swpAttributes[6]);
        tdemail1.Attributes.Add("class", swpAttributes[7]);
        txtConsigneeEmail.Width = new Unit(swpAttributes[3]);
        txtConsigneeEmail.CssClass = swpAttributes[8];

        tdtel.Height = swpAttributes[2];
        tdtel.Attributes.Add("class", swpAttributes[6]);
        tdtel1.Attributes.Add("class", swpAttributes[7]);
        txtConsigneeTel.Width = new Unit(swpAttributes[3]);
        txtConsigneeTel.CssClass = swpAttributes[8];

        tdmobil.Height = swpAttributes[2];
        tdmobil.Attributes.Add("class", swpAttributes[6]);
        tdmobil1.Attributes.Add("class", swpAttributes[7]);
        txtConsigneeMobile.Width = new Unit(swpAttributes[3]);
        txtConsigneeMobile.CssClass = swpAttributes[8];

        tdcode.Height = swpAttributes[2];
        tdcode.Attributes.Add("class", swpAttributes[6]);
        tdcode1.Attributes.Add("class", swpAttributes[7]);
        txtConsigneeZipCode.Width = new Unit(swpAttributes[3]);
        txtConsigneeZipCode.CssClass = swpAttributes[8];

        tdAddress.Height = swpAttributes[2];
        tdAddress.Attributes.Add("class", swpAttributes[6]);
        tdAddress1.Attributes.Add("class", swpAttributes[7]);
        txtConsigneeAdress.Width = new Unit(swpAttributes[3]);
        txtConsigneeAdress.CssClass = swpAttributes[8];

        tdMark.Height = swpAttributes[2];
        tdMark.Attributes.Add("class", swpAttributes[6]);
        txtRemak.Attributes.Add("class", swpAttributes[8]);

        //----------------------------------------------------------------------------------------
        tabctr1.Width = swpAttributes[0];
        tabctr1.BgColor = swpAttributes[9];

        tdsign1.Height = swpAttributes[1];
        tdsign1.Attributes.Add("class", swpAttributes[5]);
        tdsign1.Style.Add(HtmlTextWriterStyle.BackgroundImage, "url('" + swpAttributes[4] + "')");

        tdrb.Attributes.Add("class", swpAttributes[11]);
        tdrb.Height = swpAttributes[10];
        rblCarriageType.CssClass = swpAttributes[11];
        //---------------------------------------------------------------------------------------

        tabctr2.Width = swpAttributes[0];
        tabctr2.BgColor = swpAttributes[9];

        tdsign2.Height = swpAttributes[1];
        tdsign2.Attributes.Add("class", swpAttributes[5]);
        tdsign2.Style.Add(HtmlTextWriterStyle.BackgroundImage, "url('" + swpAttributes[4] + "')");

        tdPayType.Attributes.Add("class", swpAttributes[13]);
        tdPayType.Height = swpAttributes[12];
        rblPayType.CssClass = swpAttributes[13];

        tdTicket.Attributes.Add("class", swpAttributes[13]);

        //tabbank.Attributes.Add("class", swpAttributes[14]);

        //-----------------------------------------------------------------------------------------------
        divInfo.Attributes.Add("class", swpAttributes[15]);

        tabctr3.Width = swpAttributes[0];
        tabctr3.BgColor = swpAttributes[9];

        gvGood.CssClass = swpAttributes[18];
        gvGood.HeaderStyle.CssClass = swpAttributes[19];
        gvGood.PageSize = Shove._Convert.StrToInt(swpAttributes[17], 5);
        gvGood.HeaderStyle.Height = new Unit(swpAttributes[16]);
        tdtotal.Attributes.Add("class", swpAttributes[20]);

        ibNext.BackgroupImage = swpAttributes[22];
        ibUP.BackgroupImage = swpAttributes[21];
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        rblPayType.Attributes.Add("onclick", "return selectPayType()");
        InitLayOut();
        if (!IsPostBack)
        {
            //string OrderNO = "";

            //hOrderNo.Value = DAL.Procedures.P_GetOrderNO("","",ref P_GetOrderNO
            hCartType.Value = Shove._Web.Utility.GetRequest("CartType");    //哪个购物车(会员推荐专区、百企网)
            lbEMSMoney.Text = PublicFunction.GetOptionsAsString("邮费");
        }

        LoadMemberInfo();
        LoadBankInfo();
        LoadShoppingInfo();
    }

    /// <summary>
    /// 加载收款账户信息
    /// </summary>
    private void LoadBankInfo()
    {
        //lbBankName.Text = _Site.BankName;
        //lbBankNo.Text = _Site.BankCardNo;
        //lbCustomerName.Text = _Site.BankCustName;
    }

    /// <summary>
    /// 加载会员信息
    /// </summary>
    private void LoadMemberInfo()
    {
        if (_Customer != null)
        {
            txtConsigneeName.Text = _Customer.CustomerName;
            txtConsigneeEmail.Text = _Customer.Email;
            txtConsigneeTel.Text = _Customer.Tel;
            txtConsigneeMobile.Text = _Customer.Mobile;
            txtConsigneeZipCode.Text = _Customer.PostCode;
            txtConsigneeAdress.Text = _Customer.Address;
        }
    }

    /// <summary>
    /// 加载所购物品
    /// </summary>
    private void LoadShoppingInfo()
    {
        if (hCartType.Value == "carList2")
        {
            string Sum = "0";
            gvGood.DataSource = GoodOfCart.GetItems("carList2", ref Sum);
            gvGood.DataBind();

            lbSumMoney.Text = (Shove._Convert.StrToDouble(Sum, 0) + Shove._Convert.StrToDouble(lbEMSMoney.Text, 0)).ToString();
        }
        else
        {
            string Sum = "0";
            gvGood.DataSource = GoodOfCart.GetItems("carList", ref Sum);
            gvGood.DataBind();

            lbSumMoney.Text = (Shove._Convert.StrToDouble(Sum, 0) + Shove._Convert.StrToDouble(lbEMSMoney.Text, 0)).ToString();
        }
    }

    /// <summary>
    /// 增加商品到销售明细表
    /// </summary>
    /// <param name="id"></param>
    private void AddSalesDetail(long SalesID)
    {
        string Sum = "";
        ArrayList list = null;

        if (hCartType.Value == "carList2")
        {
            list = GoodOfCart.GetItems("carList2", ref Sum);
        }
        else
        {
            list = GoodOfCart.GetItems("carList", ref Sum);
        }

        for (int i = 0; i < list.Count; i++)
        {
            GoodOfCart Good = (GoodOfCart)list[i];

            double Money = Good.SumMoney + Shove._Convert.StrToDouble(lbEMSMoney.Text, Good.SumMoney);

            DAL.Procedures.P_Shop_AddSales_Detail(SalesID, Good.ID, Good.Amount, Money, Good.GoodNorm, Good.GoodColor);
        }

        string fileName = "";
        if (hCartType.Value == "carList2")
        {
            fileName = "carList2";
        }
        else
        {
            fileName = "carList";
        }
        try
        {
            HttpCookie c = HttpContext.Current.Request.Cookies[fileName];
            c.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(c);
        }
        catch { }
    }

    protected void gvGood_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Style.Add("background-image", "url(images/UserControls/bg_hui_23_2.jpg)");
        }
    }

    protected void ibNext_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            long SalesID = 0;                                                                   //销售主表 ID                
            short SalesType = 4;                                                                //商城订单 Type = 4  商城会员推荐专区订单 Type = 5
            if (hCartType.Value == "carList2")
            {
                SalesType = 5;
            }

            int PayType = Shove._Convert.StrToInt(rblPayType.SelectedValue, 0); ;               //支付方式 1 在线支付 2 网银支付 3 银行转帐 4 公司转帐
            int CarriageType = Shove._Convert.StrToInt(rblCarriageType.SelectedValue, 0);       //货运方式 1 快递(收取一定费用) 2 平邮(免费)
            bool IsTicket = false;                                                              //是否需要发票
            if (rblTicket.SelectedValue == "1")
            {
                IsTicket = true;
            }

            DAL.Procedures.P_Site_ShopAddSales_Master(_Customer.ID, _Site.ID, _Customer.ID, SalesType, txtRemak.Value, PayType, CarriageType, txtConsigneeName.Text, txtConsigneeMobile.Text, txtConsigneeTel.Text, txtConsigneeEmail.Text, txtConsigneeZipCode.Text, txtConsigneeAdress.Text, IsTicket, _Site.ID, ref SalesID);
            this.AddSalesDetail(SalesID);

            if (PayType == 1)
            {
                ibNext.Visible = false;
                this.Response.Write("<script language='javascript'>window.open('OnlinePay/Alipay02/Send2.aspx?BankCode=alipay&PayMoney=" + lbSumMoney.Text + "&RelatedOrderType=1&RelatedOrderID=" + SalesID.ToString() + "')</script>");
            }
            else if (PayType == 2)
            {
                Response.Redirect("Default.aspx?PN=Cart3&SaleID=" + SalesID.ToString());
            }
        }
    }

    protected void ibUP_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?PN=cart");
    }
}
